package threadBaseUse;

/**
 * @description: 通过实现 Runnable 接口创建线程
 * @author: Liuwanqing
 * @date: 2022-10-12 16:46
 */
public class Thread02 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        Thread thread = new Thread(dog);
        thread.start();
        Tiger tiger = new Tiger();
        ThreadProxy threadTiger = new ThreadProxy(tiger);
        threadTiger.start();

    }
}

class Dog implements Runnable{
    @Override
    public void run() {
        int count = 0;
        while (true) {
            System.out.println("汪汪~我是可爱的小狗狗~ " + "线程名：" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            count++;
            if(count == 8){
                break;
            }
        }
    }

}
class Animal{};
class Tiger extends Animal implements  Runnable{
    public void run(){
        System.out.println("嗷嗷~，我是小老虎~");
    }
}


// 使用静态代理模式实现，模拟极简的 Thread
// 我自己没有 start 方法，请代理帮忙做这件事，但是最终结果还是我
class ThreadProxy implements Runnable{
    private Runnable target = null;
    @Override
    public void run() {
        if(target != null){
            target.run(); // 动态绑定，运行类型为传入类型
        }
    }

    public ThreadProxy(Runnable target) {
        this.target = target;
    }

    public void start(){
        start0();
    }

    public void start0(){
        // 实现多线程
    }
}



